package com.helloandroid.base

import android.graphics.Rect
import android.os.Bundle
import android.view.*
import android.view.animation.Animation
import android.view.animation.ScaleAnimation
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import dai.ui.single.ScaleTo

abstract class BaseDialogFragment : DialogFragment() {

    lateinit var mView:View

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = inflater.inflate(getContentViewResId(), container, false)
        mView = view

        // 全屏 和 一些事件
        // 屏幕外取消
        dialog?.setCanceledOnTouchOutside(false)
        dialog?.setCancelable(false)
        dialog?.window?.let {
//            //这步是必须的
//            it.setBackgroundDrawableResource(android.R.color.transparent)
//            //必要，设置 padding，这一步也是必须的，内容不能填充全部宽度和高度
//            it.decorView.setPadding(0, 0, 0, 0)
//            val wlp = it.attributes
//            wlp.width = WindowManager.LayoutParams.MATCH_PARENT
//            wlp.height = WindowManager.LayoutParams.MATCH_PARENT
//            it.attributes = wlp

            // 上面会导致状态栏背景黑掉
            // 原因应该是 MATCH_PARENT的值和 decorView真实的宽高不一样导致的
            val rect =  Rect();
            val view1 = it.decorView;
            view1.getWindowVisibleDisplayFrame(rect)
            it.setBackgroundDrawableResource(android.R.color.transparent)
            val params = it.attributes
            params.width = rect.width()
            params.height = rect.height()

            // 设置动画
            //params.windowAnimations = R.anim.dialog_out
//            params.windowAnimations = 0
//            it.setWindowAnimations(0)
            it.attributes = params
        }

        return view
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initViews(mView)
        initData()

        // 添加弹出动画
        val anim = ScaleAnimation(
                0.01f,
                1f,
                0.01f,
                1f,
        Animation.RELATIVE_TO_SELF,0.5f,
        Animation.RELATIVE_TO_SELF,0.5f)
        anim.duration = 250
        mView.startAnimation(anim)
    }

    abstract fun initViews(view:View)
    abstract fun initData()
    abstract fun getContentViewResId():Int
    fun findBtn(viewId:Int):View = mView.findViewById(viewId)
    fun  <T:View> findViewById(viewId:Int): T = mView.findViewById<T>(viewId)

    // 默认消费掉,不传递给上层
    open fun backPressed():Boolean { return  true}

    override fun onResume() {
        super.onResume()
        mView.isFocusableInTouchMode = true
        mView.requestFocus()
        mView.setOnKeyListener { _, keyCode, event ->
            if(event.action == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_BACK){
                // AppUtil.toast("按了返回键")
                backPressed()
            }else{
                false
            }
        }
    }

    abstract fun dialogTag():String

    fun show(spMgr:FragmentManager){
        show(spMgr, dialogTag())
    }
}